package com.cn.wanxi.servlet.news;

import com.cn.wanxi.dto.ResultModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findNewsBySome")
public class BackNewsFindBySome extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//注意问题：乱码问题
        //        1.拿到页面数据
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String isTuijian = req.getParameter("isTuijian");
        String isTop = req.getParameter("isTop");
        String isShenhe = req.getParameter("isShenhe");
        String newsPlate = req.getParameter("newsPlate");

//        2.封装页面数据到model
        NewsModel newsModel = new NewsModel();
        newsModel.setTitle(title);
        newsModel.setContent(content);
        newsModel.setTuijian(isTuijian.equals("1"));
        newsModel.setTop(isTop.endsWith("1"));
        newsModel.setShenhe(isShenhe.endsWith("1"));
        newsModel.setNews_plate_id(newsPlate.equals("") ? 0 : Integer.parseInt(newsPlate));
//        3.调用service服务逻辑层
        INewsService iNewsService = new NewsServiceImpl();
        ResultModel model = iNewsService.test(newsModel);
//        4.返回页面数据（json）
        JSONArray jsonArray = JSONArray.fromObject(model);
        resp.getWriter().println(jsonArray);
    }
}
